home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue27 / archaeop / DinoSource / PalettePopup.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-09-16  |  5.5 KB  |  190 lines

  1. unit PalettePopup;
  2.  
  3. interface
  4.  
  5. implementation
  6.  
  7. uses
  8.   Forms, CommonStuff, Classes, Menus, SysUtils, Dialogs;
  9.  
  10. resourcestring
  11.   SPages = '&Component palette page menu items'; //Palette menu items toggle option
  12.  
  13. const
  14.   SRegPages = 'Tab page menu items'; //Registry string
  15.   SProperties = 'Configure2'; //Palette Propertites menu item
  16.   SPaletteMenuOnClick = 'PaletteLocalPopup'; //Palette menu's OnPopup handler
  17.  
  18. type
  19.   TPalettePopup = class(TObject)
  20.   private
  21.     FProperties,
  22.     FPagesOption: TMenuItem;
  23.     FPaletteList: TList;
  24.     FOldPaletteOnPopup: TNotifyEvent;
  25.   protected
  26.     procedure AddPaletteMenuItems;
  27.     procedure DeletePaletteMenuItems;
  28.     procedure DoPages(Sender: TObject);
  29.     procedure DoClick(Sender: TObject);
  30.     procedure DoPalettePopup(Sender: TObject);
  31.     procedure SetPages(Value: Boolean);
  32.   public
  33.     constructor Create;
  34.     destructor Destroy; override;
  35.   end;
  36.  
  37. constructor TPalettePopup.Create;
  38. begin
  39.   inherited Create;
  40.   //Make list for storing palette menu items
  41.   FPaletteList := TList.Create;
  42.   //Make sure there is an options menu - bear in mind
  43.   //that the other options code might not be being used
  44.   Stuff.AddOptionsItem;
  45.   //Set up component palette page options menu item
  46.   FPagesOption := NewItem(SPages, 0,
  47.     Stuff.Ini.ReadBool(SRegSection, SRegPages, False),
  48.     True, DoPages, 0, '');
  49.   //Insert the menu item
  50.   Stuff.FOptions.Add(FPagesOption);
  51.   //Set the page menu items as appropriate
  52.   SetPages(FPagesOption.Checked);
  53.  
  54.   //Chain component palette OnPopup event - this may cause problems
  55.   //if someone else chains on to it afterwards, and then we are deleted.
  56.   //The later chainer will be referring to dead code -> AV time
  57.  
  58.   //Save old OnPopup handler
  59.   FOldPaletteOnPopup := Stuff.FPalettePopup.OnPopup;
  60.   //Warn user if event was already chained
  61.   TestChainedEventHandler(TMethod(FOldPaletteOnPopup).Code,
  62.     Application.MainForm.MethodAddress(SPaletteMenuOnClick));
  63.   //Replace Delphi's event handler with our own
  64.   Stuff.FPalettePopup.OnPopup := DoPalettePopup;
  65. end;
  66.  
  67. destructor TPalettePopup.Destroy;
  68. begin
  69.   //Save option state
  70.   Stuff.Ini.WriteBool(SRegSection, SRegPages, FPagesOption.Checked);
  71.   //Delete component palette menu items
  72.   SetPages(False);
  73.   Stuff.FOptions.Free;
  74.   Stuff.FOptions := nil;
  75.   //Unchain the chained event handler
  76.   if Assigned(Stuff.FPalettePopup) then
  77.     Stuff.FPalettePopup.OnPopup := FOldPaletteOnPopup;
  78.   //Tidy up menu list
  79.   FPaletteList.Free;
  80.   inherited Destroy;
  81. end;
  82.  
  83. procedure TPalettePopup.AddPaletteMenuItems;
  84.  
  85.   procedure AddMenuItem(Menu: TMenuItem);
  86.   begin
  87.     FPaletteList.Add(Menu);
  88.     Stuff.FPalettePopup.Items.Add(Menu);
  89.   end;
  90.  
  91. var
  92.   Loop: Integer;
  93.   Item: TMenuItem;
  94. begin
  95.   //Slot an About menu item in
  96.   AddMenuItem(NewItem(SAbout, 0, False, True, Stuff.DoAbout, 0, ''));
  97.   //Add a separator
  98.   AddMenuItem(NewLine);
  99.   //Add an item for each palette page
  100.   for Loop := 0 to Stuff.FTabControl.Tabs.Count - 1 do
  101.   begin
  102.     Item := NewItem(Stuff.FTabControl.Tabs[Loop], 0, False, True, DoClick, 0, '');
  103.     Item.RadioItem := True;
  104.     //Give them some group index to make RadioItem property work
  105.     Item.GroupIndex := 57;
  106.     AddMenuItem(Item);
  107.   end;
  108.   //Add a separator
  109.   AddMenuItem(NewLine);
  110. end;
  111.  
  112. procedure TPalettePopup.DeletePaletteMenuItems;
  113. begin
  114.   //Get rid of palette tab popup menu items
  115.   while FPaletteList.Count <> 0 do
  116.   begin
  117.     TMenuItem(FPaletteList[0]).Free;
  118.     FPaletteList.Delete(0);
  119.   end;
  120. end;
  121.  
  122. procedure TPalettePopup.DoPages(Sender: TObject);
  123. begin
  124.   //Toggle existence of palette popup menu items
  125.   with Sender as TMenuItem do
  126.   begin
  127.     Checked := not Checked;
  128.     SetPages(Checked)
  129.   end
  130. end;
  131.  
  132. procedure TPalettePopup.DoClick(Sender: TObject);
  133. var
  134.   NewIndex: Integer;
  135. begin
  136.   //Work out which tab is to be selected
  137.   NewIndex := Stuff.FTabControl.Tabs.IndexOf((Sender as TMenuItem).Caption);
  138.   if NewIndex <> -1 then
  139.   begin
  140.     //Set new tab as active
  141.     Stuff.FTabControl.TabIndex := NewIndex;
  142.     //Ensure it looks like a real click by triggering OnChange event
  143.     Stuff.FTabControl.OnChange(Stuff.FTabControl)
  144.   end
  145. end;
  146.  
  147. procedure TPalettePopup.DoPalettePopup(Sender: TObject);
  148. begin
  149.   if FPagesOption.Checked then
  150.   begin
  151.     //Because the palette pages may have changed, delete the menu items...
  152.     DeletePaletteMenuItems;
  153.     //... and add new ones
  154.     AddPaletteMenuItems;
  155.     //Put the bullet mark on the currently selected page
  156.     //bearing in mind the About item and the extra new line
  157.     TMenuItem(FPaletteList.Items[Stuff.FTabControl.TabIndex + 2]).Checked := True;
  158.   end;
  159.   //Now make sure Properties lives at the end of the menu like it should
  160.   FProperties := GetComponent(Application.MainForm, SProperties, SGenericError + SProperties) as TMenuItem;
  161.   FProperties.MenuIndex := Stuff.FPalettePopup.Items.Count;
  162.   //Chain onto old OnPopup event handler
  163.   if Assigned(FOldPaletteOnPopup) then
  164.     FOldPaletteOnPopup(Stuff.FPalettePopup)
  165. end;
  166.  
  167. procedure TPalettePopup.SetPages(Value: Boolean);
  168. begin
  169.   if Value then
  170.     //This extends the local popup menu on the component palette
  171.     AddPaletteMenuItems
  172.   else
  173.     //This deletes the palette menu items
  174.     DeletePaletteMenuItems
  175. end;
  176.  
  177. var
  178.   PalettePopupObject: TPalettePopup;
  179.  
  180. initialization
  181.   try
  182.     PalettePopupObject := TPalettePopup.Create
  183.   except
  184.     on E: Exception do
  185.       ShowMessage(SSetupError + ': ' + E.Message)
  186.   end
  187. finalization
  188.   PalettePopupObject.Free
  189. end.
  190.